﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using memo;
using memoVM;

namespace memoUI
{
    /// <summary>
    /// Interaction logic for KnowledgeTestWindow.xaml
    /// </summary>
    public partial class KnowledgeTestWindow : Window
    {
        public KnowledgeTestWindow()
        {
            InitializeComponent();

            _ViewModel = ViewModel.Instance;

            Init();
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            ContentWindow Window = new ContentWindow();
            Window.Show();
            this.Close();
        }

        private void RemoveTestButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Somthing happend ¯\\_(ツ)_/¯");
        }

        private void EditTestButton_Click(object sender, RoutedEventArgs e)
        {
            if (_Test == null)
            {
                _Test = new Test();
            }

            KnowledgeTestEditorWindow Window = new KnowledgeTestEditorWindow(_Test);
            Window.ShowDialog();
            KnowledgeTestWindow window = new KnowledgeTestWindow();
            window.Show();
            this.Close();
        }

        private void NewTestButton_Click(object sender, RoutedEventArgs e)
        {
            KnowledgeTestEditorWindow Window = new KnowledgeTestEditorWindow();
            Window.ShowDialog();
            KnowledgeTestWindow window = new KnowledgeTestWindow();
            window.Show();
            this.Close();
        }

        private void ViewModeButton_Click(object sender, RoutedEventArgs e)
        {
            //Test megoldásának a mutatása vagy elrejtését változtató logika 
            if ((string)ViewModeButton.Content == "Show")
            {
                ShowAnswer();
            }
            else
            {
                ShowQuestion();
            }
        }

        private void NoIdeaButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Somthing happend ¯\\_(ツ)_/¯");
        }

        private void SoSoButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Somthing happend ¯\\_(ツ)_/¯");
        }

        private void IKnowItButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Somthing happend ¯\\_(ツ)_/¯");
        }

        private void NextTestButton_Click(object sender, RoutedEventArgs e)
        {
            _Test = _ViewModel.GetNextTest();
            ShowQuestion();
        }

        private void Init()
        {
            NoIdeaButton.IsEnabled = false;
            SoSoButton.IsEnabled = false;
            IKnowItButton.IsEnabled = false;
            RemoveTestButton.IsEnabled = true;
            EditTestButton.IsEnabled = true;
            NewTestButton.IsEnabled = true;

            LoadTest();
        }

        private void LoadTest()
        {
            int numberOfTests = _ViewModel.GetNumberOfTests();

            if (numberOfTests > 0)
            {
                _Test = _ViewModel.GetSelectedTest();
                ShowQuestion();
            }
            else if (numberOfTests == 0)
            {
                TestTextBlock = new TextBlock();
                TestTextBlock.Text = "Something Happend ¯\\(ツ)/¯";
                TestTextBlock.VerticalAlignment = VerticalAlignment.Center;
                TestTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(TestTextBlock, 1);
                MiddleGrid.Children.Add(TestTextBlock);
            }            
        }

        private void ShowAnswer()
        {
            ViewModeButton.Content = "Hide";
            NoIdeaButton.IsEnabled = true;
            SoSoButton.IsEnabled = true;
            IKnowItButton.IsEnabled = true;
            RemoveTestButton.IsEnabled = false;
            EditTestButton.IsEnabled = false;
            NewTestButton.IsEnabled = false;

            TestTextBlock.Visibility = Visibility.Hidden;
            TestImage.Visibility = Visibility.Hidden;

            if (_Test.AnswerType == Test.TestType.String)
            {
                TestTextBlock = new TextBlock();
                TestTextBlock.Text = (string)_Test.Answer;
                TestTextBlock.VerticalAlignment = VerticalAlignment.Center;
                TestTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(TestTextBlock, 1);
                TestTextBlock.Visibility = Visibility.Visible;
                MiddleGrid.Children.Add(TestTextBlock);
            }
            else
            {
                TestImage = new Image();
                TestImage.Source = new BitmapImage(new Uri(@"Images/" + _Test.Answer, UriKind.RelativeOrAbsolute));
                TestImage.VerticalAlignment = VerticalAlignment.Stretch;
                TestImage.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid.SetRow(TestTextBlock, 1);
                TestImage.Visibility = Visibility.Visible;
                MiddleGrid.Children.Add(TestImage);
            }
        }

        private void ShowQuestion()
        {
            ViewModeButton.Content = "Show";
            NoIdeaButton.IsEnabled = false;
            SoSoButton.IsEnabled = false;
            IKnowItButton.IsEnabled = false;
            RemoveTestButton.IsEnabled = true;
            EditTestButton.IsEnabled = true;
            NewTestButton.IsEnabled = true;

            TestTextBlock.Visibility = Visibility.Hidden;
            TestImage.Visibility = Visibility.Hidden;

            if (_Test.QuestionType == Test.TestType.String)
            {
                TestTextBlock = new TextBlock();
                TestTextBlock.Text = (string)_Test.Question;
                TestTextBlock.VerticalAlignment = VerticalAlignment.Center;
                TestTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(TestTextBlock, 1);
                TestTextBlock.Visibility = Visibility.Visible;
                MiddleGrid.Children.Add(TestTextBlock);
            }
            else
            {
                TestImage = new Image();
                TestImage.Source = new BitmapImage(new Uri(@"Images/" + _Test.Question, UriKind.RelativeOrAbsolute));
                TestImage.VerticalAlignment = VerticalAlignment.Stretch;
                TestImage.HorizontalAlignment = HorizontalAlignment.Stretch;
                Grid.SetRow(TestTextBlock, 1);
                TestImage.Visibility = Visibility.Visible;
                MiddleGrid.Children.Add(TestImage);
            }
        }

        private ViewModel _ViewModel;
        private Test _Test;
        private TextBlock TestTextBlock = new TextBlock();
        private Image TestImage = new Image();
    }
}
